-- 撑杆僵尸类
local Zombie = require("app.mode.Zombie")
local AniHelper = require("app.utils.AnimateHelper")

local PoleZombie = {}
setmetatable(PoleZombie, Zombie)
PoleZombie.__index = PoleZombie

function PoleZombie:initView()
    local name
    if self:isJumped() then
        name = self:getName()
    else
        name = self:getName() .. "_nojump"
    end
    if self._body_state == Zombie.NOHEAD then
        name = name .. "_losthead"
    end
    self._sprite, self._animate = AniHelper("zombies/", name)
end

function PoleZombie:exportData()
    local data = Zombie.exportData(self)
    data.y = data.y - 30
    return data
end

-- 重写执行动画函数
function PoleZombie:runAnimate()
    -- 跳跃后的动作跟其它僵尸一致
    if self:isJumped() then
        Zombie.runAnimate(self)
    else
        self._sprite:stopAllActions()
        local sprite

        if self:getActionState() == Zombie.MOVING then
            -- 跳跃前移动
            sprite, self._animate = AniHelper("zombies/", self:getName() .. "_nojump")
            self._sprite:setSpriteFrame(sprite:getSpriteFrame())
            self._sprite:runAction(cc.Spawn:create(cc.MoveBy:create(960 / self:getMoveSpeed(), cc.p(-960, 0)), self._animate))
        elseif self:getActionState() == Zombie.JUMPING then
            -- 跳跃完成
            local function finishJump(node, data)
                -- 跳跃之后变为正常移动
                self._jump_target = nil
                self:setMoveSpeed(self:getMoveSpeed() * 0.8)
                self:setJumped(true)
                self:setActionState(Zombie.MOVING)
                self:runAnimate()
            end

            -- 二段跳
            local function secondJump(node, data)
                if self._jump_target and self._jump_target:getName() == "tallnut" then
                    finishJump()
                else
                    sprite, self._animate = AniHelper("zombies/", self:getName() .. "_jump_2", 0.1, 1)
                    self._sprite:setSpriteFrame(sprite:getSpriteFrame())
                    self._sprite:setPositionX(self._sprite:getPositionX() - 110)
                    self._sprite:runAction(cc.Sequence:create(self._animate, cc.CallFunc:create(finishJump), nil))
                end
            end

            -- 一段跳
            sprite, self._animate = AniHelper("zombies/", self:getName() .. "_jump", 0.1, 1)
            self._sprite:setSpriteFrame(sprite:getSpriteFrame())
            self._sprite:runAction(cc.Sequence:create(self._animate, cc.CallFunc:create(secondJump), nil))
        end
    end
end

-- 被攻击
function PoleZombie:beAttack(attack, effect)
    -- 如果被打断了头，则不能再跳跃
    if self:getBlood() - attack <= self:getMaxBlood() * 0.2 and self._body_state ~= Zombie.NOHEAD then
        self:setJumped(true)
    end
    Zombie.beAttack(self, attack, effect)
end

-- 攻击植物
function PoleZombie:attack(p)
    -- 记录状态有没有变化
    local b_change

    -- 遇到第一棵植物时不攻击，进行跳跃
    if not self:isJumped() then
        if (p:getName() == "potatomine" or p:getName() == "chomper") and p:isReady() then
            p:addTarget(self)
            p:beAttack()
            return
        end
        b_change = self:getActionState() ~= Zombie.JUMPING
        self:setActionState(Zombie.JUMPING)
        self._jump_target = p
    else
        b_change = self:getActionState() ~= Zombie.ATTACKING
        self:setActionState(Zombie.ATTACKING)
        if self._target == nil then
            self._target = p
            p:addTarget(self)
        end
    end

    -- 如果状态有变化则更新动画
    if b_change then
        self:runAnimate()
    end
end

function PoleZombie:explode()
    self._sprite:setPositionY(self._sprite:getPositionY() - 30)
    Zombie.explode(self)
end

function PoleZombie:setPosition(x, y)
    if self._sprite then
        if type(x) == "table" then
            self._sprite:setPosition(x.x, x.y + 30)
        elseif type(x) == "number" and type(y) == "number" then
            self._sprite:setPosition(x, y + 30)
        end
    end
end

function PoleZombie:isJumped()
    return self._data.hasJumped
end

function PoleZombie:setJumped(val)
    self._data.hasJumped = val
end

return PoleZombie
